package cn.zhage.aimin.canal.client;

import cn.zhage.aimin.canal.client.annotation.CanalTableMapping;
import cn.zhage.aimin.canal.client.handler.EntityHandler;
import jakarta.annotation.PostConstruct;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*
 *
 * @author 渣哥
 */
public class EntityHandlerHolder {

    private final Map<String, EntityHandler<?>> entityHandlerMap = new ConcurrentHashMap<>();


    private final List<EntityHandler<?>> entityHandlerList;


    public EntityHandlerHolder(List<EntityHandler<?>> entityHandlerList) {
        this.entityHandlerList = entityHandlerList;
    }

    @PostConstruct
    public void init(){
        if(entityHandlerList != null && !entityHandlerList.isEmpty()){
            for(EntityHandler<?> entityHandler : entityHandlerList){
                CanalTableMapping annotation = entityHandler.getClass().getAnnotation(CanalTableMapping.class);
                if(annotation != null){
                    String tableName = annotation.value();
                    entityHandlerMap.put(tableName,entityHandler);
                }
            }
        }
    }


    public EntityHandler<?> getEntityHandler(String tableName){
        return entityHandlerMap.get(tableName);
    }
}
